package gui;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import core.common.Configurations;
import core.exceptions.DBAccessException;
import core.objects.Author;
import core.objects.Book;
import core.objects.Category;
import core.objects.Publisher;
import db.DBAccessEngine;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author abadi
 */
public class JPanelBookSearch extends JPanelSearch {

    private static final String LETS_FIND_A_BOOK = "Lets find a book!";
    public static final String FINDA_BOOK = "Find-a-Book";

    public JPanelBookSearch() {
    }

    @Override
    void search() {
        super.search();
        jPanelContents.setVisible(false);
        jPanelContents.setVisible(true);

        DBAccessThread.invoke(new IDBAccessThread() {
            @Override
            public Object onCreateQuery() throws DBAccessException {
                Set<Book> books = (Set<Book>) searchBookWithKeyword(jTextFieldSearch.getText());
                return books;
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onAccessFinished(Object response) {

                Set<Book> books = (Set<Book>) response;
                for (Book book : books) {
                    jPanelContents.add(new JPanelBookInfo(book));
                }
                jLabelSearchLoading.setIcon(null);
                jPanelContents.setVisible(false);
                jPanelContents.setVisible(true);
                if (jPanelContents.getComponents().length == 0) {
                    jPanelContents.add(GUIUtils.getNotFoundPanel());
                }
            }

            @Override
            public void onAccessFailed(DBAccessException e) {
                jLabelSearchLoading.setIcon(null);
                GUIUtils.showMessageError(e, jPanelContents);
            }
        });
    }

    void search(Book book) {
        super.search();
        jPanelContents.add(new JPanelBookInfo(book));
        jLabelSearchLoading.setIcon(null);
        jPanelContents.setVisible(false);
        jPanelContents.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    private Collection<Book> searchBookWithKeyword(String searchStr) {
        try {
            Set<Book> allBooks = new HashSet<>();
            List<Book> books = (List<Book>) DBAccessEngine.Book().selectContains(searchStr, true);
            allBooks.addAll(books);
            if (allBooks.size() < Configurations.getBookSearchLimit()) {
                books = (List<Book>) DBAccessEngine.Book().selectContains(searchStr, new Category(0, ""), true);
                allBooks.addAll(books);
            }
            if (allBooks.size() < Configurations.getBookSearchLimit()) {
                books = (List<Book>) DBAccessEngine.Book().selectContains(searchStr, new Author(0, ""), true);
                allBooks.addAll(books);
            }
            if (allBooks.size() < Configurations.getBookSearchLimit()) {
                books = (List<Book>) DBAccessEngine.Book().selectContains(searchStr, new Publisher(0, ""), true);
                allBooks.addAll(books);
            }

            for (Book book : allBooks) {
                GUIUtils.setBookPrameters(book, this);
            }
            return allBooks;
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
            jLabelSearchLoading.setIcon(null);
        }
        return null;
    }

    @Override
    void setComponentsInitialTextValues() {
        initialSearchBoxValue = LETS_FIND_A_BOOK;
        initialSearchFormHeaderText = FINDA_BOOK;
    }
}
